package webapp;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class DeletePurchase
 */
public class DeletePurchase extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ServletConfig config;
       
    
	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		this.config=config;
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		System.out.println("We are in Service method of DeletePurchase");
		PrintWriter out = response.getWriter();
		
		HttpSession session=request.getSession(false); 
		String un = (String) session.getAttribute("name");
		
        if(un!=null){
		
		String todelete=request.getParameter("deleteid");
		
		
		String msg=" ";
		// Connection conn = null;
				 String url = "jdbc:mysql://localhost:3306/";
				 String dbName = "webappschema";
				 String driver = "com.mysql.jdbc.Driver";
				 String dbusername = "root";
				 String dbpassword = "webapp";
				 List dataList = new ArrayList(); 
				 
				 try {Class.forName(driver).newInstance();
				 Connection conn = DriverManager.getConnection(url+dbName,dbusername,dbpassword);
				 String strQuery = ("Delete from purchase where Purchaseid= '" + todelete + "' " );
				 Statement st = conn.createStatement();
				 int rs = st.executeUpdate(strQuery);
				 response.setContentType("text/html");
				 if(rs > 0) 
				 {
					   out.println("<script type=\"text/javascript\">");
		    	 	   out.println("alert('Record successfully deleted');");
		    	 	   out.println("location='View.jsp';");
		    	 	   out.println("</script>");
					 //msg = "Record Deleted Successfully";
				     
					 //response.setContentType("text/html");
					 //PrintWriter out1 = response.getWriter();
					 //out1.println("<font size = '6' color=red>" + msg + "</font>");
				        
				    }
				 
				 
				 //rs.close();
				 st.close();
			 	 } catch (Exception e) {
			 	   e.printStackTrace();
			 	 }
        }
				 else{  
						out.print("Please login first");    
			            request.getRequestDispatcher("index.jsp").include(request, response);  
			        } 
			        out.close();
				 
}


}
